package com.yangguang.huahui.utils;

import android.util.Log;

import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2017/3/1.
 */

public class SignUtil {
    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
        sortMap.putAll(map);

        return sortMap;
    }

    public static String getSign(Map<String, String> map) {
        String md5_key ="";
        String key;
        String value;
        Map<String, String> resultmap = sortMapByKey(map);
        for (Map.Entry<String, String> entry : resultmap.entrySet()) {
            key = entry.getKey();
            value = entry.getValue();
            md5_key += key + "=" + value + "&";
        }
        String sign = CryptoUtil.md5(md5_key + "userKey");
        Log.e("md5_key", md5_key + "userKey");
        Log.e("sign", "------>" + sign);
        return sign;
    }
}
